/**
 * zhanjun
 */

$(function() {
	
	/*
	 * 获取传入的参数
	 */
	var idRole=getUrlParamValue("idRole");
	if(idRole==""){
		alert("传入的参数不正确，请重新打开！");
		location.href='role-admin.html';
	}

	$.getJSON("../role/ser_load.do?id="+idRole, function(data) {	
		$('#sprolename').html(data.roleName);	
	});

	
	
	$("#idRole").attr("value",idRole);

	/*
	 * 载入下拉树
	 */
	var loadTreebox=function(idResource){
		
		$('#treebox').tree({
			checkbox:true,
			cascadeCheck:true,
			 url:'../privilege/ser_getResTree.do?idResource='+idResource
		});
	};
/*
 * 绑定系统选择下拉事件
 */
	
	$('#idSystem').bind('change', function() {
		if($("#idSystem").val()!=""){
			var idsys=$("#idSystem").val();
			loadTreebox(idsys);
		}
	});

	$("#idSystem")[0].options.length = 0;
	
	$.getJSON("../privilege/ser_getSystems.do", function(data) {
		$("<option value=''>--请选择--</option>").appendTo("#idSystem");
		$.each(data, function(i) {
			$("<option value='" + data[i].idResource + "'>"+ data[i].resName + "</option>").appendTo("#idSystem");
		});
	});
	
	$("#opetList").html("");
	$.getJSON("../privilege/ser_getOperations.do", function(data) {	
		$.each(data.rows, function(i) {
			  var line="<tr><td><input type='checkbox' name='idOperation' id='idOperation' value='"+data.rows[i].idOperation+"'/>"+data.rows[i].opetName+"["+data.rows[i].opetKey+"]</td></tr>";
			
			  $(line).appendTo("#opetList");
		
		});
	});
	var mygrid;
	var loadTempPrivilege=function(roleid){		
		//展示临时表权限
		mygrid = new dhtmlXGridObject('gridbox');
		mygrid.setImagePath("../component/dhtmlx25/imgs");
		mygrid.setHeader("角色,资源名称,操作");
		mygrid.setInitWidths("100,200,100");
		mygrid.setColAlign("center,left,center");
		mygrid.setColTypes("ed,ed,ed");
	   // mygrid.getCombo(5).put(2,2);
		mygrid.setColSorting("str,str,str");
		//mygrid.setColumnColor("white,#d5f1ff,#d5f1ff")
	    mygrid.setSkin("modern");
		mygrid.init();
		mygrid.loadXML("../privilege/ser_LoadPrivGrid.do?idRole="+roleid+"&type=temp");
	};
	loadTempPrivilege(idRole);
	var loadPrivilege =function(roleid)
	{	
		pomygrid = new dhtmlXGridObject('prowerGridbox');
		mygrid.setImagePath("../component/dhtmlx25/imgs");
		pomygrid.setHeader("角色,资源名称,操作");
		pomygrid.setInitWidths("100,200,100");
		pomygrid.setColAlign("center,left,center");
		pomygrid.setColTypes("ed,ed,ed");
	   // pomygrid.getCombo(5).put(2,2);
		pomygrid.setColSorting("str,str,str");
		//pomygrid.setColumnColor("white,#d5f1ff,#d5f1ff")
	    pomygrid.setSkin("modern");
		pomygrid.init();
		pomygrid.loadXML("../privilege/ser_LoadPrivGrid.do?idRole="+roleid+"&type=real");
	};
	loadPrivilege(idRole);
	
	$("#btnpsave").click(function() {		
		//保存权限到正式库
		var postdata="idRole="+idRole;
		
		$.ajax({
			type : "POST",
			url : "../privilege/ser_savePrivilege.do",
			data : postdata,
			success : function(msg) {
			
				alert(msg);
				loadTempPrivilege(idRole);	//刷新临时表	
				loadPrivilege(idRole);	//刷新正式表
			}
		});
	});
	$("#btnTempDel").click(function() {
		//删除临时权限权限
		if(mygrid.getSelectedId()==null)
		{
			alert("请先在列表中先选择要删除的记录。");
			return;
		}
		var rId = mygrid.getSelectedId();
		mygrid.setRowTextBold(rId);
		mygrid.setRowTextStyle(rId, "text-decoration: line-through;");
		var postdata="idRole="+rId;
		$.ajax({
			type : "POST",
			url : "../privilege/ser_deleteTempPrivilege.do",
			data : postdata,
			success : function(msg) {
				alert(msg);
				loadTempPrivilege(idRole);	//刷新临时表	
			}
		});
	});
	$("#btnTempRefresh").click(function() {
		//刷新临时权限
		loadTempPrivilege(idRole);
	});
	$("#btnDelPrivilege").click(function() {		
		//删除权限
		if(pomygrid.getSelectedId()==null)
		{
			alert("请先在列表中先选择要删除的记录。");
			return;
		}
		var rId = pomygrid.getSelectedId();
		pomygrid.setRowTextBold(rId);
		pomygrid.setRowTextStyle(rId, "text-decoration: line-through;");
		var postdata="idRole="+rId;
		$.ajax({
			type : "POST",
			url : "../privilege/ser_deletePrivilege.do",
			data : postdata,
			success : function(msg) {
				alert(msg);
				loadTempPrivilege(idRole);	//刷新临时表	
			}
		});
		
	});
	$("#btnRefreshPrivilege").click(function() {		
		//刷新列表
		loadPrivilege(idRole);	
	});
	$("#btncreatePower").click(function() {
		//生成权限
		var nodes=$('#treebox').tree('getChecked');
		var resnodes=new Array();

		if(nodes.length==0){
			alert('请选择资源节点.');
			return false;
		}else{			
			for(var i=0;i<nodes.length;i++){
				resnodes.push(nodes[i].id);
			}			
		}
	
		
		var idoptns=document.getElementsByName("idOperation");
		var optAry=new Array();
		for(var i=0;i<idoptns.length;i++)
		{
			if(idoptns[i].checked)
				optAry.push(idoptns[i].value);
		}
		var idRole=document.getElementById("idRole").value;
		var idSystem=document.getElementById("idSystem").value;
		var postdata="idRole="+idRole+"&idSystem="+idSystem+"&idOperation="+optAry.join(",")+"&idResource="+resnodes.join(",");
	
		if($('#dataform').form('validate')){		
			$.ajax({
				type : "POST",
				url : "../privilege/ser_createPrivilegeTemp.do",
				data : postdata,
				success : function(msg) {
					alert(msg);
					loadTempPrivilege(idRole);			
				}
			});
		}	
	});
	
});
